<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form onsubmit="return false">
        <textarea id="m" style="height: 300px;width: 300px"></textarea>
        <button type="button" value="发送消息" onclick="send(this.form.m.value)">发送消息</button>

        <textarea id="presponseText" style="height: 300px;width: 300px"></textarea>
        <button type="button" value="清空内容" onclick="document.getElementById('presponseText').value=''">清空内容</button>
    </form>
</body>
</html>

<script>
    var socket;
    if (window.WebSocket){

        socket=new WebSocket("ws://localhost:10000/hello");

        socket.onmessage=function (ev) {
            var rt=document.getElementById("presponseText");
            rt.value=rt.value+ "\n"|ev.data;
        }

        socket.onopen=function (ev) {
            var rt=document.getElementById("presponseText");
            rt.value="连接开启了";
        }

        socket.onclose=function (ev) {
            var rt=document.getElementById("presponseText");
            rt.value="连接关闭了";
        }
    }else{
        alert("当前浏览器不支持websocket")
    }

    function send(message) {

        if (!window.socket){
            return;
        }
        if (socket.readyState==WebSocket.OPEN){

            console.log(socket)
            socket.send(message)
        }else
        {
            console.log("连接未开启")
        }
    }
</script>